<template>
    <transition name="modal-fade" appear>
        <div v-if="isVisible" class="modal-overlay" @click="handleOverlayClick">
            <transition name="modal-zoom" appear>
                <div class="modal-container" @click.stop :style="modalStyle">
                    <!-- 整个弹窗作为一个图片背景 -->
                    <div class="modal-image-background" :style="backgroundStyle">
                        <!-- 关闭按钮区域 -->


                        <!-- 自定义内容插槽 -->
                        <slot name="content"></slot>

                        <!-- 自定义按钮插槽 -->
                        <slot name="buttons"></slot>
                    </div>
                </div>
            </transition>

            <div v-if="showCloseButton" class="close-btn-area" @click="closeModal"></div>
        </div>
    </transition>
</template>

<script setup>
import { computed, defineEmits, defineProps } from 'vue'

const props = defineProps({
    // 弹窗显示状态
    isVisible: {
        type: Boolean,
        default: false
    },
    // 背景图片路径
    backgroundImage: {
        type: String,
        required: true
    },
    // 是否显示关闭按钮
    showCloseButton: {
        type: Boolean,
        default: true
    },
    // 是否点击遮罩层关闭
    closeOnOverlay: {
        type: Boolean,
        default: true
    },
    modalStyle: {
        type: Object,
        default: () => ({})
    }
})

const emit = defineEmits(['close'])

// 计算背景样式
const backgroundStyle = computed(() => ({
    width: '100%',
    height: '100%',
    background: `url('${props.backgroundImage}') no-repeat center center`,
    backgroundSize: 'contain',
    position: 'relative'
}))

// 关闭弹窗
const closeModal = () => {
    emit('close')
}

// 处理遮罩层点击
const handleOverlayClick = () => {
    if (props.closeOnOverlay) {
        closeModal()
    }
}
</script>

<style lang="scss" scoped>
// 引入自定义字体
@font-face {
    font-family: 'AlimamaFangYuanTi';
    src: url('../assets/images/阿里妈妈方圆体 VF.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

// 动画效果
.modal-fade-enter-active,
.modal-fade-leave-active {
    transition: opacity 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
    opacity: 0;
}

.modal-zoom-enter-active,
.modal-zoom-leave-active {
    transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-zoom-enter-from,
.modal-zoom-leave-to {
    opacity: 0;
    transform: scale(0.7) translateY(-50px);
}

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.82);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-container {
    position: relative;
    width: 100%;
    height: 100%;
}

.modal-image-background {
    width: 100%;
    height: 100%;
    position: relative;
}

// 关闭按钮区域
.close-btn-area {
    position: absolute;
    top: 3%;
    right: 2%;
    width: 50px;
    height: 50px;
    background: url('../assets/images/2/close.png') no-repeat center center;
    background-size: contain;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        transform: scale(1.1);
        background-image: url('../assets/images/2/close_active.png');
    }
}

// 全局字体样式
* {
    font-family: 'AlimamaFangYuanTi', sans-serif;
}
</style>